package com.wx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.PageParam;
import com.wx.common.SecurityUser;
import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.domain.dto.DayReportQueryParam;
import com.wx.domain.dto.SysPatientFollowDto;
import com.wx.domain.vo.*;
import com.wx.entity.SysPatientFollow;
import com.wx.holder.SecurityUserHolder;
import com.wx.service.SysPatientFollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * <p>Title:      SysPatientFollowController. </p>
 * <p>Description  </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author yangwensheng@100doc.com.cn
 * @since 2021-03-03 11:36:07
 */
@RestController
@RequestMapping(value = "/sysPatient/follow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "随访管理", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysPatientFollowController {

    @Resource
    private SysPatientFollowService sysPatientFollowService;

    @Resource
    private SecurityUserHolder securityUserHolder;

    /**
     * 分页查询随访列表
     *
     * @param pageParam PageParam
     * @return PageInfo
     */
    @PostMapping("/list/page")
    @ApiOperation(httpMethod = "POST", value = "分页查询随访列表")
    public Wrapper<PageInfo<PatientFollowVo>> listPage(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody PageParam<SysPatientFollowDto> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<PatientFollowVo> sysPatientFollows = sysPatientFollowService.listAllPage(pageParam.getParam());
        PageInfo<PatientFollowVo> pageInfo = new PageInfo<>(sysPatientFollows);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }

    /**
     * 更新
     *
     * @param sysPatientFollow SysPatientFollow
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "更新")
    public Wrapper<Integer> update(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody SysPatientFollow sysPatientFollow) {
        int result = sysPatientFollowService.updateInfo(sysPatientFollow);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * 删除随访记录
     *
     * @param id String
     * @return Integer
     */
    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "删除随访记录")
    public Wrapper<Integer> deletePatientFollow(@PathVariable("id") String id) {
        SecurityUser user = securityUserHolder.getUser();
        int result = sysPatientFollowService.deletePatientFollow(id, user.getUserId(), user.getRealName());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * 多线程查询
     *
     * @param sysPatientFollow SysPatientFollowDto
     */
    @PostMapping(value = "/multi/thread/detail")
    @ApiOperation(httpMethod = "POST", value = "多线程查询明细")
    public Wrapper<List<PatientFollowDetailVo>> multiThreadQueryDetail(@RequestBody SysPatientFollowDto sysPatientFollow) throws Exception {
        List<PatientFollowDetailVo> sysPatientFollows = sysPatientFollowService.multiThreadQueryDetail(sysPatientFollow);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, sysPatientFollows);
    }

    /**
     * 导出随访明细
     *
     * @param response         HttpServletResponse
     * @param sysPatientFollow SysPatientFollowDto
     * @throws IOException
     */
    @PostMapping(value = "/export/detail")
    @ApiOperation(httpMethod = "POST", value = "导出随访明细")
    public void exportDetail(HttpServletResponse response, @ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody SysPatientFollowDto sysPatientFollow) throws IOException {
        List<PatientFollowDetailVo> sysPatientFollows = sysPatientFollowService.listAllDetail(sysPatientFollow);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("随访明细.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), PatientFollowDetailVo.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("随访明细").doWrite(sysPatientFollows);
    }


    /**
     * 导出问询明细
     *
     * @param response         HttpServletResponse
     * @param sysPatientFollow SysPatientFollowDto
     * @throws IOException
     */
    @PostMapping(value = "/export/inquiry/detail")
    @ApiOperation(httpMethod = "POST", value = "导出问询明细")
    public void exportInquiryDetail(HttpServletResponse response, @ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody SysPatientFollowDto sysPatientFollow) throws IOException {
        List<PatientInquiryDetailVo> sysPatientFollows = sysPatientFollowService.listInquiryDetail(sysPatientFollow);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("问询明细.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), PatientInquiryDetailVo.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("问询明细").doWrite(sysPatientFollows);
    }

    /**
     * 查询随访时间轴
     *
     * @param patientId String
     * @return List<PatientFollowVo>
     */
    @PostMapping("/list/{patientId}")
    @ApiOperation(httpMethod = "POST", value = "查询随访时间轴")
    public Wrapper<List<PatientFollowVo>> list(@PathVariable String patientId) {
        List<PatientFollowVo> sysPatientFollows = sysPatientFollowService.listByPatientId(patientId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, sysPatientFollows);
    }

    @PostMapping("/refresh")
    @ApiOperation(httpMethod = "POST", value = "刷数据")
    public Wrapper<?> refresh(@RequestBody List<String> patientIds) {
        sysPatientFollowService.refresh(patientIds);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

    /**
     * 导出处方统计相关数据
     *
     * @param response            HttpServletResponse
     * @param dayReportQueryParam DayReportQueryParam
     * @throws IOException
     */
    @PostMapping(value = "/export/prescription/statistics")
    @ApiOperation(httpMethod = "POST", value = "导出处方统计相关数据")
    public void exportPrescriptionStatistics(HttpServletResponse response, @RequestBody DayReportQueryParam dayReportQueryParam) throws IOException {
        List<PrescriptionStatisticsVo> sysPatientFollows = sysPatientFollowService.exportPrescriptionStatistics(dayReportQueryParam);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("处方统计.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), PrescriptionStatisticsVo.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("处方统计").doWrite(sysPatientFollows);
    }

    /**
     * 导出随访统计相关数据
     *
     * @param response            HttpServletResponse
     * @param dayReportQueryParam DayReportQueryParam
     * @throws IOException
     */
    @PostMapping(value = "/export/statistics")
    @ApiOperation(httpMethod = "POST", value = "导出随访统计相关数据")
    public void exportStatistics(HttpServletResponse response, @RequestBody DayReportQueryParam dayReportQueryParam) throws IOException {
        List<FollowStatistics> sysPatientFollows = sysPatientFollowService.exportStatistics(dayReportQueryParam);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("随访统计.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), FollowStatistics.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("随访统计").doWrite(sysPatientFollows);
    }

}